<?php
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
		
class Authentication {
	var $_id_user = 0;
	var $_login = "";
	var $_password = "";
	var $_role = "";
	var $_id_aff = "";
	var $_auth = FALSE;
	function Authentication($auto = TRUE)
	{
		if($auto)
		{
			$CI =& get_instance();
			if($this->login($CI->session->userdata('usr_name'), $CI->session->userdata('usr_password')))
			{
				$this->_id_user = $CI->session->userdata('usr_id');
				$this->_login = $CI->session->userdata('usr_name');
				$this->_password = $CI->session->userdata('usr_password');
				$this->_role = $CI->session->userdata('usr_role');
				$this->_slug = $CI->session->userdata('usr_slug');
			}
		}
	}
	function getId(){return $this->_id_user;}
	function getLogin(){return $this->_login;}
	function getPassword(){return $this->_password;}
	function getRol(){return $this->_role;}
	function getSlug(){return $this->_slug;}
	
	function login($username= "", $password = "")
	{
		if(empty($username)||empty($password))
			return FALSE;

		$CI =& get_instance();		

		$sql = "SELECT * FROM `users` WHERE `usr_username`=? AND `usr_password`=? and `usr_active`='1'";
		$query = $CI->db->query($sql, array($username, $password));

		//login ok
		if($query->num_rows()==1)
		{
			$row = $query->row();

			$CI->session->set_userdata('usr_id', $row->usr_id);//id_user//userid
			$this->_id_user = $row->usr_id;
			$CI->session->set_userdata('usr_name', $row->usr_username);//name//username
			$this->_login = $username;
			$CI->session->set_userdata('usr_password', $password);
			$this->_password = $row->usr_password;
			$CI->session->set_userdata('usr_role', $row->usr_role);
			$this->_role= $row->role;
			$CI->session->set_userdata('usr_slug', $row->usr_slug);
			$this->_slug= $row->usr_slug;
			
            //----oo00oo  set user password 
			//$CI->session->set_userdata('fbuserid', $row->fbuserid);
			//$this->_fbuserid = $row->fbuserid;					
			
			//$CI->session->set_userdata('group', $row->user_group_id);
			//$this->_group = $row->user_group_id;
			
			$id_user = $this->_id_user = $row->usr_id;
			$roles = $this->_role = $row->usr_role;
			
			$ip = $_SERVER['REMOTE_ADDR']; 
			$dat=date("Y-m-d G:i:s");
			
			//$sql = "INSERT INTO user_log (user_id, roles, user_login_time,user_logout_time,ipaddress) VALUES ('".$user_id."', '".$roles ."','".$dat."', '', '".$ip."')";
			//$query = $CI->db->query($sql);

			$this->_auth = TRUE;

			return TRUE;
		}
		else
		{
			$this->_auth = FALSE;
			$this->logout();

			return FALSE;
		}
	}
	
	//function to create cookie to session persistence
/*	function rememberMe(){
		$CI =& get_instance();
		//generic value
		$expire_time = '21600';
		$prefix = '';
		$domain_url = base_url();
		$path = '/';
		//users data
		$id_n = 'id_user';
		$id_v = $this->getId();
		
		$user_n = 'username';
		$user_v = $this->getLogin();
		
		$pass_n = 'password';
		$pass_v = $this->getPassword();
		
		$rol_n = 'role';
		$rol_v = $this->getRol();
		
		$aff_n = 'id_aff';
		$aff_v = $this->getAff();
		//cookies data
		//$this->load->helper('cookie');
		$id_user = array(
						'name' => $id_n,
						'value' => $id_v,
						'expire' => $expire_time,
						'domain' => $domain_url,
						'path' => $path,
						'prefix' => $prefix,
						);
		$user_name = array(
						'name' => $user_n,
						'value' => $user_v,
						'expire' => $expire_time,
						'domain' => $domain_url,
						'path' => $path,
						'prefix' => $prefix,
						);
		$user_password = array(
						'name' => $pass_n,
						'value' => $pass_v,
						'expire' => $expire_time,
						'domain' => $domain_url,
						'path' => $path,
						'prefix' => $prefix,
						);
		$user_rol = array(
						'name' => $rol_n,
						'value' => $rol_v,
						'expire' => $expire_time,
						'domain' => $domain_url,
						'path' => $path,
						'prefix' => $prefix,
						);
		$id_affiliate = array(
						'name' => $aff_n,
						'value' => $aff_v,
						'expire' => $expire_time,
						'domain' => $domain_url,
						'path' => $path,
						'prefix' => $prefix,
						);
		set_cookie($id_user);
		set_cookie($user_name);
		set_cookie($user_password);
		set_cookie($user_rol);
		set_cookie($id_affiliate);

	}
	//verify cookies to create the session or not
	function getRemember(){
		//users data
		$id_n = 'id_user';
		$user_n = 'username';
		$pass_n = 'password';
		$rol_n = 'role';
		$aff_n = 'id_aff';
		
		//get cookie data
		
		$CI =& get_instance();
		
		if(get_cookie($id_n)){
			$user = get_cookie($id_n);
			($user['value'] != '')? $CI->session->set_userdata('id_user', $user) : '';
		}else{
			
		}
		
		if(get_cookie($user_n)){
			$name = get_cookie($user_n);
			($user['value'] != '')? $CI->session->set_userdata('name', $name) : '';
		}
		
		if(get_cookie($pass_n)){
			$password = get_cookie($pass_n);
			($user['value'] != '')? $CI->session->set_userdata('password', $password) : '';
		}
		
		if(get_cookie($rol_n)){
			$role = get_cookie($rol_n);
			($user['value'] != '')? $CI->session->set_userdata('role', $role) : '';
		}
		
		if(get_cookie($aff_n)){
			$aff = get_cookie($aff_n);
			($user['value'] != '')? $CI->session->set_userdata('id_affiliate', $aff) : '';
		}
	}*/
	
	function logout()
	{
		$CI =& get_instance();
		$CI->session->sess_destroy();        
		
		/*$id_n = 'id_user';
		$user_n = 'username';
		$pass_n = 'password';
		$rol_n = 'role';
		$aff_n = 'id_aff';
		
		delete_cookie($id_n);
		delete_cookie($user_n);
		delete_cookie($pass_n);
		delete_cookie($rol_n);
		delete_cookie($aff_n);*/
	}
    function isLogin(){
		$CI =& get_instance();
		$usuario_login = $CI->session->userdata('usr_name');
		$usuario_password = $CI->session->userdata('usr_password');
		if (empty($usuario_login) && empty($usuario_password)) {
		   return FALSE;
		}else return TRUE;
	}
    
	function check($level = 0, $strict = TRUE)
	{
		if(!$this->_auth)
			return FALSE;

		if($strict)
		{
			if($level == $this->_level)
				return TRUE;
			else
				return FALSE;
		}
		else
		{
			if($level <= $this->_level)
				return TRUE;
			else
				return FALSE;
		}
	}
        	
	
	/*
	function verifyLogin($action=""){
		if (!$this->session->userdata('login')) {
		
			redirect("admin/login");
		}
	}*/
}
?>